package com.clippick.setting;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.net.ConnectivityManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import co.kr.clippick.R;

import com.clippick.MainActivity;
import com.clippick.api.GetApi;
import com.clippick.common.Toasts;
import com.clippick.common.Utils;

public class FeedbackActivity extends Activity implements
		OnCheckedChangeListener, OnClickListener {
	// common
	private Context mContext;
	private MainActivity parent;
	private View view;
	public ConnectivityManager mananger;

	// data:info
	private SharedPreferences pref;
	private SharedPreferences.Editor editor;

	// view
	private RadioGroup radioGroup;
	private LinearLayout osLayer;
	private RadioButton happyButton, normalButton, angryButton;
	private CheckBox macCheckBox, androidCheckBox, winCheckBox, iOSCheckBox;
	private EditText commentEditText;
	private Button submitButton;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.feedback);
		mContext = this;
		
		if (Utils.isAndroidLowerThanICS()) {
			// remove title
			requestWindowFeature(Window.FEATURE_NO_TITLE);
		}

		radioGroup = (RadioGroup) findViewById(R.id.feedback_radio_group);
		osLayer = (LinearLayout) findViewById(R.id.checkLayer);

		commentEditText = (EditText) findViewById(R.id.comment);

		submitButton = (Button) findViewById(R.id.submit);
		submitButton.setOnClickListener(this);

		happyButton = (RadioButton) findViewById(R.id.happy);
		normalButton = (RadioButton) findViewById(R.id.normal);
		angryButton = (RadioButton) findViewById(R.id.angry);
		happyButton.setOnCheckedChangeListener(this);
		normalButton.setOnCheckedChangeListener(this);
		angryButton.setOnCheckedChangeListener(this);

		macCheckBox = (CheckBox) findViewById(R.id.check_mac);
		androidCheckBox = (CheckBox) findViewById(R.id.check_and);
		winCheckBox = (CheckBox) findViewById(R.id.check_win);
		iOSCheckBox = (CheckBox) findViewById(R.id.check_ios);
		macCheckBox.setOnCheckedChangeListener(this);
		androidCheckBox.setOnCheckedChangeListener(this);
		winCheckBox.setOnCheckedChangeListener(this);
		iOSCheckBox.setOnCheckedChangeListener(this);

		// default check
		androidCheckBox.setChecked(true);
	}

	private String getComment() {
		return this.commentEditText.getText().toString();
	}

	private String getOS() {
		List<String> list = new ArrayList<String>();
		for (int i = 0; i < osLayer.getChildCount(); i++) {
			CheckBox osCheckBox = (CheckBox) osLayer.getChildAt(i);
			if (osCheckBox.isChecked()) {
				String os = "";
				switch (osCheckBox.getId()) {
				case R.id.check_mac:
					os = "mac";
					break;
				case R.id.check_and:
					os = "android";
					break;
				case R.id.check_win:
					os = "win";
					break;
				case R.id.check_ios:
					os = "ios";
					break;
				}
				list.add(os);
			}
		}

		String com = ",";
		StringBuilder builder = new StringBuilder();
		for (int i = 0; i < list.size(); i++) {
			if (i == list.size() - 1) {
				com = "";
			}
			builder.append(list.get(i).toString()).append(com);
		}

		list.clear();
		list = null;
		Log.i("feedback", builder.toString() + "");
		return builder.toString();
	}

	private String getFeel() {
		String result = "";
		for (int i = 0; i < radioGroup.getChildCount(); i++) {
			RadioButton button = (RadioButton) radioGroup.getChildAt(i);
			if (button.isChecked()) {
				switch (button.getId()) {
				case R.id.happy:
					result = "1";
					break;
				case R.id.normal:
					result = "2";
					break;
				case R.id.angry:
					result = "3";
					break;
				}
			}
		}
		return result;
	}

	private boolean submit() {
		new SubmitTask().execute();
		return true;
	}

	@Override
	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		// TODO Auto-generated method stub
		switch (buttonView.getId()) {
		case R.id.check_mac:
			Log.i("check", "mac" + isChecked);
			if (isChecked) {
				buttonView.setButtonDrawable(R.drawable.about_mac_hi);
			} else {
				buttonView.setButtonDrawable(R.drawable.about_mac_lo);
			}
			break;

		case R.id.check_and:
			if (isChecked) {
				buttonView.setButtonDrawable(R.drawable.about_android_hi);
			} else {
				buttonView.setButtonDrawable(R.drawable.about_android_lo);
			}
			break;

		case R.id.check_win:
			if (isChecked) {
				buttonView.setButtonDrawable(R.drawable.about_windows_hi);
			} else {
				buttonView.setButtonDrawable(R.drawable.about_windows_lo);
			}
			break;

		case R.id.check_ios:
			if (isChecked) {
				buttonView.setButtonDrawable(R.drawable.about_ios_hi);
			} else {
				buttonView.setButtonDrawable(R.drawable.about_ios_lo);
			}
			break;

		case R.id.happy:
			if (isChecked) {
				buttonView.setButtonDrawable(R.drawable.ifeel_happy_hi);
			} else {
				buttonView.setButtonDrawable(R.drawable.ifeel_happy_lo);
			}
			break;

		case R.id.normal:
			if (isChecked) {
				buttonView.setButtonDrawable(R.drawable.ifeel_normal_hi);
			} else {
				buttonView.setButtonDrawable(R.drawable.ifeel_normal_lo);
			}
			break;

		case R.id.angry:
			if (isChecked) {
				buttonView.setButtonDrawable(R.drawable.ifeel_angry_hi);
			} else {
				buttonView.setButtonDrawable(R.drawable.ifeel_angry_lo);
			}
			break;
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.submit:
			this.submit();
			break;
		}
	}

	// asynchronous http request
	public class SubmitTask extends AsyncTask<String, String, String> {
		private ProgressDialog mProgress;
		private boolean isSent = false;
		private boolean feedbackEmtpy = true;
		private ConnectivityManager manager;

		protected void onPreExecute() {
			manager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
			mProgress = new ProgressDialog(mContext);
			mProgress.setMessage("Loading");
			mProgress.show();
		}

		@Override
		protected String doInBackground(String... value) {
			GetApi api = new GetApi();

			// check null value
			if (!getComment().equals("")) {
				// check network status is available, otherwise show toasts
				// "try again please"
				if (Utils.checkNetwokState(manager)) {
					// store success == true, fail = false
					isSent = api.sendFeedback(getFeel(), getOS(), getComment());
				} else {
					isSent = false;
				}
			} else {
				// comment is empty
				feedbackEmtpy = false;
			}
			return null;
		}

		protected void onPostExecute(String unused) {
			mProgress.dismiss();
			if (isSent) {
				Toasts.showToast(mContext, "Thank you!");
				finish();
			} else {
				if (feedbackEmtpy)
					Toasts.showToast(mContext, "try again please");
				else
					Toasts.showToast(mContext, "please give your comment");
			}
		}
	}
}
